﻿//==============================================================================
// File: ThemeService.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System.Collections.Generic;
using System.Windows;
using WpfRcp.Core.Common;
using WpfRcp.Core.Globals;
using WpfRcp.Core.Services;
using WpfRcp.Resources;
using Microsoft.Practices.Composite.Events;

namespace WpfRcp.Services
{
    public class ThemeService : BindableBase, IThemeService
    {
        private readonly IEventAggregator m_EventAggregator;
        private readonly IApplicationSettings m_ApplicationSettings;

        private static readonly IList<Theme> SupportedThemesList = new List<Theme>
        {
            ApplicationConstants.Themes.LightTheme,
            ApplicationConstants.Themes.DarkTheme
        };
        
        public static readonly ResourceDictionary LightTheme = new Light();
        public static readonly ResourceDictionary DarkTheme = new Dark();

        public ThemeService(IEventAggregator eventAggregator, IApplicationSettings applicationSettings)
        {
            m_EventAggregator = eventAggregator;
            m_ApplicationSettings = applicationSettings;
            m_ApplicationSettings.DefaultsRestored += delegate { CurrentTheme = (Theme) m_ApplicationSettings.CurrentTheme; };
        }

        public IList<Theme> SupportedThemes
        {
            get { return SupportedThemesList; }
        }

        public Theme CurrentTheme
        {
            get { return (Theme) m_ApplicationSettings.CurrentTheme; }
            set
            {
                if (value != null)
                {
                    m_ApplicationSettings.CurrentTheme = value.Name;
                    SetTheme(value);
                    OnPropertyChanged("CurrentTheme");
                }
            }
        }

        public void SetTheme(Theme theme)
        {
            Application.Current.Resources.MergedDictionaries.Remove(DarkTheme);
            Application.Current.Resources.MergedDictionaries.Remove(LightTheme);

            switch (theme.Name)
            {
                case ApplicationConstants.Themes.Dark:

                    Application.Current.Resources.MergedDictionaries.Add(DarkTheme);
                    break;

                case ApplicationConstants.Themes.Light:

                    Application.Current.Resources.MergedDictionaries.Add(LightTheme);
                    break;

                default:

                    Application.Current.Resources.MergedDictionaries.Add(DarkTheme);
                    break;
            }

            // Publish event
            OnThemeChanged();
        }

        private void OnThemeChanged()
        {
            m_EventAggregator.GetEvent<ThemeChangedEvent>().Publish(CurrentTheme);
        }
    }
}